import {} from '../constants'
import _ from 'lodash'

import {effects} from 'dva/saga'
import Map from 'lodash.map'
import Cookie from 'js-cookie'

export function rImage(w, h, t) {
    return `https://dummyimage.com/${w}x${h}/000/fffx`
}

// console.log(effects.put)
export function isImage(file) {
    var ImgType = ["gif", "jpeg", "jpg", "bmp", "png"];//图片
    if (!RegExp("\.(" + ImgType.join("|") + ")$", "i").test(file.name.toLowerCase())) {
        console.log("不是指定图片格式,重新选择");
        return false;
    } else {
        return true
    }
}


function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}

export function uuid() {
    // return (S4() + S4() + S4()+ S4() + S4() + S4() + S4() + S4());
    var date = new Date();
    var y = (date.getFullYear()) + "";
    var m = ((date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + ""
    var d = (date.getDate()) + ""
    var h = (date.getHours()) + "";
    var M = ((date.getMinutes()) < 10 ? "0" + (date.getMinutes()) : date.getMinutes()) + ""
    var s = ((date.getSeconds()) < 10 ? "0" + (date.getSeconds()) : date.getSeconds()) + ""
    var _uuid = (y + m + d + "-" + h + M + s + "-" + S4() + S4());
    return _uuid
}

export function isMatch(myreg, str) {
    //声明邮箱正则
    //对输入的值进行判断
    return myreg.test(str);
}

export function setStateReducer(state, actions) {
    var {payload, isStrict} = actions
    if (!isStrict) {
        var merge = require('lodash.merge');
        state = merge(state, payload)
        var a = Object.assign({}, state);
        if (NODE_ENV != 'build') {
            console.log(Object.keys(payload), a)
        }
        return a
    } else {
        return payload
    }

}


export function setState(obj, isStrict = false) {
    return (name_space) => {
        return effects.put({
            type: `${name_space}/setState`,
            payload: obj,
            isStrict
        })
    }
}

export function setHistory(history) {
    window.__history__ = history;
    return
}

export function getHistory() {
    return window.__history__;
}

export function* getState(namespace) {
    var result = yield effects.select(state => state[namespace]);
    return Object.assign({}, result)
}


export function findInArray(array, filter) {
    var index = require("lodash.findindex")(array, filter)
    return array[index]
}


export function arrayToCollection(array) {
    var c = {}
    Map(array, (item) => {
        c[item.id] = item
    })
    return c
}

export function getFileExtension(filename) {
    var index1 = filename.lastIndexOf(".");
    var index2 = filename.length;
    var postf = filename.substring(index1, index2);//后缀名
    return postf
}


export function unloging() {
    var self = this;
    var Cookie = require('js-cookie')
    try {
        self.user = JSON.parse(Cookie.get("cookie_user"))
    } catch (e) {
        window.location.replace("/")
    }
}

export function getUrlParams(search) {
    var search = search || location.search;	//获取location的search属性，保存在search中
    var params = {};		//创建空对象params
    if (search != "") {		//如果search不是空字符串
        search.slice(1).split("&").forEach(	//?username=zhangdong&pwd=123456;//search去开头?，按&切割为数组，forEach
            function (val) {
                var arr = val.split("=");		//将当前元素值按=切割，保存在arr中
                params[arr[0]] = arr[1];		//向params中添加一个元素,属性名为arr[0],值为arr[1]
            }
        );
    }
    return params;
}

export function getUser() {
    var user = null
    try {
        return user = JSON.parse(Cookie.get('cookie_user'));
    } catch (e) {
        return {
            defaultShopId: ""
        }
    }
}

export function getPageScroll() {
    var xScroll, yScroll;
    if (self.pageYOffset) {
        yScroll = self.pageYOffset;
        xScroll = self.pageXOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
        yScroll = document.documentElement.scrollTop;
        xScroll = document.documentElement.scrollLeft;
    } else if (document.body) {// all other Explorers
        yScroll = document.body.scrollTop;
        xScroll = document.body.scrollLeft;
    }
    var arrayPageScroll = new Array(xScroll, yScroll);
    return arrayPageScroll;
};



